<?php


namespace app\api\controller\v1;


use app\common\model\ClueErrorLog;
use app\lib\exception\ParamException;
use other\MessageSend;
use think\Db;
use think\Log;
use think\Queue;

class ClueLog
{


    /* 跟进备注 */
    public function clueRemark()
    {

        $post = file_get_contents("php://input");
//        Log::record(json_encode($post,JSON_UNESCAPED_UNICODE), 'error');
//        Log::record($_GET, 'error');

        $params = json_decode($post, true);

//        dump($params);die;
//        $insertLog = [
//            'type' => '1',
//            'adv_id' => isset($params['adv_id']) ? $params['adv_id'] : '',
//            'clue_id' => isset($params['clue_id']) ? $params['clue_id'] : '',
//            'param' => json_encode($_GET, JSON_UNESCAPED_UNICODE),
//            'data' => json_encode($params, JSON_UNESCAPED_UNICODE),
//            'create_time' => date('Y-m-d H:i:s')
//        ];
//        Db::name('clue_api_log_action')->insert($insertLog);
//        $access_token = $_GET['access_token'];

        $params = json_decode($post, true);

//        $sign=api_get_sign($params,$access_token);
//        dump($sign);die;


//        if (empty($params['adv_id'])) {
//            return Json(['msg' => '账号不能为空：adv_id', 'code' => 40001]);
//        }

        if (empty($params['clue_id'])) {
            return Json(['msg' => '线索ID不能为空：clue_id', 'code' => 40001]);
        }

        if (empty($params['remark'])) {
            return Json(['msg' => '备注内容不能为空：remark', 'code' => 40001]);
        }


//        $isAid = Db::name('account')->where('adv_id', $params['adv_id'])->find();
//        if (empty($isAid)) {
//            return Json(['msg' => '账户ID错误', 'code' => 40001]);
//        }

        $isClue = model('clueLog')->where(['clue_id' => $params['clue_id']])->find();
        if (empty($isClue)) {
            return Json(['msg' => '线索错误', 'code' => 40001]);
        }

        $dataLog = [
            'clue_log_id' => $isClue['id'],
            'clue_id' => $isClue['clue_id'],
            'content' => $params['remark'],
            'create_time' => time(),
            'update_time' => time(),
            'source' => 2,
        ];

        Db::name('clue_progress')->insert($dataLog);

        return Json(['code' => 200, 'msg' => '提交成功']);

    }

    /* 跟进状态 */
    public function clueStatus()
    {

        $post = file_get_contents("php://input");
//        Log::record(json_encode($post,JSON_UNESCAPED_UNICODE), 'error');
//        Log::record($_GET, 'error');

        $params = json_decode($post, true);

//        dump($params);die;
        $insertLog = [
            'type' => '2',
            'adv_id' => isset($params['adv_id']) ? $params['adv_id'] : '',
            'clue_id' => isset($params['clue_id']) ? $params['clue_id'] : '',
            'param' => json_encode($_GET, JSON_UNESCAPED_UNICODE),
            'data' => json_encode($params, JSON_UNESCAPED_UNICODE),
            'create_time' => date('Y-m-d H:i:s')
        ];
        Db::name('clue_api_log_action')->insert($insertLog);
//        $access_token = $_GET['access_token'];

        $params = json_decode($post, true);

//        $sign=api_get_sign($params,$access_token);
//        dump($sign);die;


        if (empty($params['adv_id'])) {
            return Json(['msg' => '账号不能为空：adv_id', 'code' => 40001]);
        }

        if (empty($params['clue_id'])) {
            return Json(['msg' => '线索ID不能为空：clue_id', 'code' => 40001]);
        }

        if (empty($params['status'])) {
            return Json(['msg' => '备注内容不能为空：status', 'code' => 40001]);
        }

        $status=Db::name('clue_progress_status')->where('id',$params['status'])->find();
        if(empty($status)||$status['status']!=1||!empty($status['delete_time'])){
            return Json(['msg' => 'status错误', 'code' => 40001]);
        }

        $isAid = Db::name('account')->where('adv_id', $params['adv_id'])->find();
        if (empty($isAid)) {
            return Json(['msg' => '账户ID错误', 'code' => 40001]);
        }

        $isClue = model('clueLog')->where(['clue_id' => $params['clue_id'], 'adv_id' => $params['adv_id']])->find();
        if (empty($isClue)) {
            return Json(['msg' => '线索错误', 'code' => 40001]);
        }

        model('clueLog')->where('id',$isClue['id'])->update(['progress_status'=>$params['status']]);

        return Json(['code' => 200, 'msg' => '提交成功']);

    }

    public function clueLevelStatus()
    {

        $post = file_get_contents("php://input");
//        Log::record(json_encode($post,JSON_UNESCAPED_UNICODE), 'error');
//        Log::record($_GET, 'error');

        $params = json_decode($post, true);

//        dump($params);die;
        $insertLog = [
            'type' => '3',
            'adv_id' => isset($params['adv_id']) ? $params['adv_id'] : '',
            'clue_id' => isset($params['clue_id']) ? $params['clue_id'] : '',
            'param' => json_encode($_GET, JSON_UNESCAPED_UNICODE),
            'data' => json_encode($params, JSON_UNESCAPED_UNICODE),
            'create_time' => date('Y-m-d H:i:s')
        ];
        Db::name('clue_api_log_action')->insert($insertLog);
//        $access_token = $_GET['access_token'];

        $params = json_decode($post, true);

//        $sign=api_get_sign($params,$access_token);
//        dump($sign);die;

        if (empty($params['adv_id'])) {
            return Json(['msg' => '账号不能为空：adv_id', 'code' => 40001]);
        }

        if (empty($params['clue_id'])) {
            return Json(['msg' => '线索ID不能为空：clue_id', 'code' => 40001]);
        }


        if(!in_array($params['level'],[9999,0,1,2,3,4,5])){
            return Json(['msg' => '星级参数错误', 'code' => 40001]);
        }

        $isAid = Db::name('account')->where('adv_id', $params['adv_id'])->find();
        if (empty($isAid)) {
            return Json(['msg' => '账户ID错误', 'code' => 40001]);
        }

        $isClue = model('clueLog')->where(['clue_id' => $params['clue_id'], 'adv_id' => $params['adv_id']])->find();
        if (empty($isClue)) {
            return Json(['msg' => '线索错误', 'code' => 40001]);
        }

        model('clueLog')->where('id',$isClue['id'])->update(['level_status'=>$params['level']]);

        return Json(['code' => 200, 'msg' => '提交成功']);

    }

}